﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNA_Game_Framework
{
	public class WindowVar : Container
	{
		String title;
		Theme.WindowVar theme;

		public Container Container
		{
			get { return subwindows[0]; }
		}

		public Rectangle HeaderBox
		{
			get { return new Rectangle((int)ScreenPosition.X, (int)ScreenPosition.Y, (int)Size.X, HeaderHeight); }
		}

		public int HeaderHeight
		{
			get { return (int)(theme.titleFont.MeasureString(title).Y + 10 + theme.headerBorder.bordertop.Height + theme.headerBorder.borderbottom.Height); }
		}

		public WindowVar(string name, object parent, string title, Vector2 position, Vector2 size, Theme.WindowVar theme)
			: base(name, parent, position, size)
		{
			this.title = title;
			this.theme = theme;

			this.AddBorder("", new Vector2(0, 0), new Vector2(HeaderBox.Width, HeaderBox.Height), Border.Sides.All, theme.headerBorder.Name);
			Vector2 headerBackgroundSize = new Vector2(HeaderBox.Width - theme.headerBorder.borderleft.Width - theme.headerBorder.borderright.Width, HeaderBox.Height - theme.headerBorder.bordertop.Height - theme.headerBorder.borderbottom.Height);
			this.AddTileImage("", new Vector2(theme.headerBorder.borderleft.Width, theme.headerBorder.borderright.Height), theme.headerBackground, headerBackgroundSize);
			int halfLineSeperation = (int)(HeaderBox.Height - theme.headerBorder.bordertop.Height - theme.headerBorder.borderbottom.Height - TextTools.Height(title, theme.titleFont)) / 2;
			Vector2 linePosition = new Vector2(theme.headerBorder.borderleft.Width + halfLineSeperation, theme.headerBorder.bordertop.Height + halfLineSeperation);
			this.AddLabel("", linePosition, title, theme.titleFont);

			Vector2 subWindowPosition = new Vector2(theme.windowBorder.borderleft.Width, HeaderHeight);
			int subWindowWidth = (int)size.X - theme.windowBorder.borderleft.Width - theme.windowBorder.borderright.Width;
			int subWindowHeight = (int)size.Y - HeaderHeight - theme.windowBorder.borderbottom.Height;
			Vector2 subWindowSize = new Vector2(subWindowWidth, subWindowHeight);

			Vector2 borderSize = new Vector2(size.X, size.Y - HeaderHeight);
			this.AddBorder("", new Vector2(0, HeaderHeight), borderSize, Border.Sides.TopLess, theme.windowBorder.Name);
			this.AddTileImage("", subWindowPosition, theme.background, subWindowSize);

			this.AddSubwindow(this.name + "Content", subWindowPosition, subWindowSize);
		}

		internal override bool OnMouseLeftDragged(MouseEventArgs e)
		{
			if (HeaderBox.Contains(e.LastX, e.LastY))
			{
				relativePosition.X += e.X - e.LastX;
				relativePosition.Y += e.Y - e.LastY;

				if (relativePosition.X < 0)
				{
					relativePosition.X = 0;
				}
				if (relativePosition.Y < 0)
				{
					relativePosition.Y = 0;
				}
				if (parent is Screen)
				{
					if (relativePosition.X + Width > ((Screen)parent).Width)
					{
						relativePosition.X = ((Screen)parent).Width - Width;
					}
					if (relativePosition.Y + Height > ((Screen)parent).Height)
					{
						relativePosition.Y = ((Screen)parent).Height - Height;
					}
				}
				if (parent is Container)
				{
					if (relativePosition.X + Width > ((Container)parent).Width)
					{
						relativePosition.X = ((Container)parent).Width - Width;
					}
					if (relativePosition.Y + Height > ((Container)parent).Height)
					{
						relativePosition.Y = ((Container)parent).Height - Height;
					}
				}

				return true;
			}
			return false;
		}

		#region Region Draw
		internal override void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch)
		{
			if (Visible)
			{
				DrawControls(graphics, spriteBatch);
				DrawSubWindows(graphics, spriteBatch);
			}
		}
		#endregion
	}
}
